//var info = require('../info.js');
var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {

    ex_tab_index: 0,    
    data: { msg: '获取验证码', mobileno: '', code: '',},
    code:'',
    mobileno: '',
    isAgree: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      isAgree: '0',
    });
  },
  isAgreement: function () {
    var that = this
    var isAgree = that.data.isAgree
    if (isAgree == '0') {
      that.setData({
        isAgree: '1',
      });
    } else {
      that.setData({
        isAgree: '0',
      });
    }
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  //转发
  onShareAppMessage: function () {
    let users = wx.getStorageSync('user');
    if (res.from === 'button') { }
    return {
      title: 'A1智连',
      path: 'pages/pageIndex/pageIndex',
      success: function (res) { }
    }
  },
  getCode(){
    var that=this;
    var emailno=that.data.data.emailno;
    var isAgree = that.data.isAgree
    if (!that.checkEmail(emailno)){
      wx.showToast({
        title: '请输入正确邮箱！',
        icon: 'success',
        duration: 2000
      })
      return;
    }
    if (that.data.data.msg == "获取验证码" || that.data.data.msg == "重新获取验证码"){
      that.setData({
        "data.msg": 60,
      })
      var a = setInterval(function () {
        // console.log(1)
        if (that.data.data.msg == 0) {
          that.setData({
            "data.msg": "重新获取验证码",
          });
          clearInterval(a);
        } else {
          that.setData({
            "data.msg": that.data.data.msg - 1
          })
        }
        var code=that.data.data.code
       // console.log('code='+code)
        if (null != code && code!=''){
          that.setData({
            "data.msg": "获取验证码",
          });
          clearInterval(a);
        }
      }, 1000)

      //调用pico接口
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress+'/Attendee/user/sendcode'
      var t = {}
      t["v"] = '1.0.0'
      t["client"] = 'ios'
      t["timestamp"] = timestamp
      t["to"] = emailno
      t["sendtype"] = '0'
      t["lan"] = 'cn'
      var sign = app.globalData.picotoken + 'clientiossendtype0timestamp' + timestamp + 'to' + emailno + app.globalData.picotoken 
      console.log('sign=' + sign)
      var b64 = md5.hexMD5(sign)
      b64 = b64.toUpperCase()
      console.log('b64=' + b64)
      //   console.log('slb64=' + slb64)
      t["sign"] = b64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: t,
        success(res) {
          console.log(res)
        }, fail: res => {
          //验证码发送失败
          console.log(res, "error")
        }
      })
    }
    // console.log(that.checkPhone(mobileno));
    
  },
  inputChange(e){
    var that=this;
    var tmp=that.data.data;
    var m=e.currentTarget.dataset.name
    tmp[m] = e.detail.value
    // console.log(m)
    that.setData({
      data: tmp
    })
    // console.log(that.data)
  }, 
  signInBtn(e){
    var that = this;
    var emailno = that.data.data.emailno;
    var code = that.data.data.code
    var isAgree = that.data.isAgree
    console.log("rawdata", e)
    if (!that.checkEmail(emailno)) {
      wx.showToast({
        title: '请输入正确邮箱！',
        icon: 'success',
        duration: 2000
      })
    }else if(code==''|| null==code){
      wx.showToast({
        title: '请输入验证码！',
        icon: 'success',
        duration: 2000
      })
    } else if (isAgree == '0' || null == isAgree) {
      wx.showToast({
        title: '请阅读网络协议!',
        icon: 'success',
        duration: 2000
      })
    }else{
      //获取授权（缓存openid）
      wx.getUserInfo({
        success: (res) => {
          console.log(res)
          this.setData({
            userInfo: res.userInfo
          });
        }
      })
      //调用pico接口,缓存会展id,用户id
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress +'/Attendee/user/login'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["username"] = emailno
      k["verificationcode"] = code
      k["logintype"] = '0'
      var sign = app.globalData.picotoken + 'clientioslogintype0timestamp' + timestamp + 'username' + emailno+'verificationcode' + code + app.globalData.picotoken
      var curb64 = md5.hexMD5(sign)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          var curdata = mesg.data
          //缓存展会id，用户id
          var exhibitionid = curdata.exhibitionid
          var u = curdata.u
          wx.setStorageSync('exhibitionid', exhibitionid);
          wx.setStorageSync('u', u);
          wx.redirectTo({
            url: '../aiIndex/aiIndex',
          })
        }, fail: res => {
          wx.redirectTo({
            url: '../aiIndex/aiIndex',
          })
          console.log(res, "error")
        }
      })
      console.log("登录！！！")
    }
  },
  getPhoneNumber: function (e) {
    console.log("fffffffffffffffff")
    var that = this;
    //获取用户信息(code)
    var curcode = ''
    wx.login({
      success: res => {
        console.log('gghhhhhhhhhhhhhh')
        if (res.code) {
          console.log(res);
          console.log('code=' + res.code);
          curcode = res.code
          // that.globalData.code = res.code;
        }
        else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
        //获取IV
        console.log('iv=' + e.detail.iv)
        //获取encryptedData
        console.log('curcode=' + curcode)
        console.log('获取用户登录态失败！' + res.errMsg)
        //调用pico接口,缓存
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        var url = app.globalData.picoaddress + '/attendee/user/register'
        var k = {}
        k["v"] = '1.0.0'
        k["client"] = 'ios'
        k["timestamp"] = timestamp
        k["lan"] = 'cn'
        k["nickname"] = '1'
        k["photo"] = '1'
        k["jscode"] = curcode
        k["encryptedData"] = e.detail.encryptedData
        k["iv"] = e.detail.iv

        var sign = app.globalData.picotoken + 'clientios' + 'encryptedData' + e.detail.encryptedData + 'iv' + e.detail.iv + 'jscode' + curcode + 'nickname1photo1timestamp' + timestamp + app.globalData.picotoken
        var curb64 = md5.hexMD5(sign)
        k["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: k,
          success(res) {
            console.log(res)
            var mesg = res.data
            var curdata = mesg.data
            //缓存展会id，用户id
            var exhibitionid = curdata.exhibitionid
            var u = curdata.u
            wx.setStorageSync('exhibitionid', exhibitionid);
            wx.setStorageSync('u', u);
            wx.redirectTo({
              url: '../aiIndex/aiIndex',
            })
          }, fail: res => {
            console.log(res, "error")
          }
        })
      }
    })

  },
  checkEmail(num) {//检查邮箱是否合法 
    if (!(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(num))) {
      // console.log("邮箱有误，请重填");
      return false;
    }
    return true;
  },
  toPhoneSignIn: function () {
    wx.reLaunch({
      url: '../signInbyPhone/signIn'
    })
  },
  toScanSignIn: function () {
    wx.reLaunch({
      url: '../scanSignIn/scanSignIn'
    })
  },
  warning(msg) {
    wx.showModal({
      title: '提示',
      content: msg,
      success: function (res) {
      }
    })
  },
})